﻿using sys = System;
using System.Collections.Generic;
using sysconsole = System.Console;

namespace SplitOS.OSAL.INTERNAL
{
    public static class Out
    {
        public static void Write(object value, sys.ConsoleColor color = sys.ConsoleColor.White)
        {
            SaveColor();
            SetColor(color);
            sysconsole.Write(value);
            RestoreColor();
        }
        public static void WriteTitle(object value, sys.ConsoleColor color = sys.ConsoleColor.White)
        {
            SaveBackColor();
            SetBackColor(color);
            sysconsole.Write(value);
            RestoreBackColor();
        }
        public static void WriteLine(object value, sys.ConsoleColor color = sys.ConsoleColor.White)
        {
            SaveColor();
            SetColor(color);
            sysconsole.WriteLine(value);
            RestoreColor();
        }
        private static sys.ConsoleColor colorBGBackup;
        public static void SaveBackColor() { colorBGBackup = sysconsole.BackgroundColor; }
        public static void SetBackColor(sys.ConsoleColor color) { sysconsole.BackgroundColor = color; }
        public static void RestoreBackColor() { sysconsole.BackgroundColor = colorBGBackup; }
        private static sys.ConsoleColor colorFGBackup;
        public static void SaveColor() { colorFGBackup = sysconsole.ForegroundColor; }
        public static void SetColor(sys.ConsoleColor color) { sysconsole.ForegroundColor = color; }
        public static void RestoreColor() { sysconsole.ForegroundColor = colorFGBackup; }
    }

    public static class StringTools
    {
        public static bool StartsWith(string __str, string __expression)
        {
            string str = "";
            for (int i = 0; i < (__expression.Length); i++)
            {
                str += __str[i];
                if (str == __expression) return true;
            }
            return false;
        }
        public static bool EndsWith(string __str, string __expression)
        {
            string str = "";
            for (int i = ((__str.Length-1)-(__expression.Length-1)); i == (__str.Length-1); i++)
            {
                str += __str[i];
                if (str == __expression) return true;
            }
            return false;
        }
    }


    public static class Variables
    {
        private static List<string> names = new List<string>();
        private static List<object> values = new List<object>();
        public static bool AddVariable(string __name, object __value)
        {
            names.Add(__name);
            values.Add(__value);
            return true; // 0 = Variable successfully added
        }
        public static object GetVariable(string __name)
        {
            foreach (string name in names)
            {
                if (name == __name) return values[names.IndexOf(__name, 0)];
            }
            return null; // nothing found
        }
    }
}